﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasyGo.Domain.Model;
using System.Data.SqlClient;
using System.Data;
using EasyGo.Domain.Projections;

namespace EasyGo.Data.Dao.Impl
{
    public class UserAccountDao : SqlDao, IUserAccountDao
    {
        public UserInfo ValidateUser(string email, string pass)
        {
            using (var cmd = this.GetStoredProcedureCommand("UserAccount_Validate"))
            {
                cmd.Parameters.AddWithValue("@Email", email);
                cmd.Parameters.AddWithValue("@Pass", pass);
                using(IDataReader reader = cmd.ExecuteReader())
                {
                    var userInfo = reader.ReadSingleOrDefault<UserInfo>();
                    reader.Close();
                    return userInfo;
                }
            }
        }

        public int CreateUser(UserAccount command)
        {
            using (var cmd = this.GetStoredProcedureCommand("UserAccount_Create"))
            {
                cmd.Parameters.AddWithValue("@FirstName", command.FirstName);
                cmd.Parameters.AddWithValue("@LastName", command.LastName);
                cmd.Parameters.AddWithValue("@CompanyId", command.CompanyId);
                cmd.Parameters.AddWithValue("@Email", command.Email);
                cmd.Parameters.AddWithValue("@Phone", command.Phone);
                cmd.Parameters.AddWithValue("@Info", command.Info);
                cmd.Parameters.AddWithValue("@Pass", command.Pass);
                cmd.Parameters.AddWithValue("@UserRoleId", (int)command.UserRole);
                return (int)cmd.ExecuteScalar();
            }
        }

        public IEnumerable<ListUser> GetAllListUsers()
        {
            using (var cmd = this.GetStoredProcedureCommand("UserAccount_GetAll"))
            {
                return cmd.ExecuteReader().ReadAll<ListUser>();
            }
        }


        public UserAccount GetUser(int id)
        {
            using (var cmd = this.GetStoredProcedureCommand("UserAccount_Get"))
            {
                cmd.Parameters.AddWithValue("@Id", id);
                return cmd.ExecuteReader().ReadSingleOrDefault<UserAccount>();
            }
        }
    }
}
